package com.iteye.trafficRecorder;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.content.Context;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

public class GPSHelper {
	
	private LocationManager locationManager;
	
	private int satelliteCount = 0;
	
	private Location currentLocation;
	
	private boolean stop;
	
	private LocationListener gpsListener=null;
	
	private LocationListener networkListner=null;
	
	private String TAG = "GPSHelper";
	
	public GPSHelper(Context context) {
		locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
		locationManager.addGpsStatusListener(listener);
		networkListner=new MyLocationListener();
		gpsListener=new MyLocationListener();
		stop = true;
	}
	
	public void start() {
		if (stop) {
			stop = false;
			registerLocationListener();
			requestLocation();
		}
	}
	
	public void stop() {
		if (!stop) {
			stop = true;
			removeLocation();
		}
	}
	
	public int getSatelliteCount() {
		return satelliteCount;
	}

	public Location getCurrentLocation() {
		return currentLocation;
	}

	private void requestLocation() {
		//获取最后一次位置信息
		while (true) {
			Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
			if (location != null) {
				currentLocation = location;
				break;
			}
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				Log.w("Location", e.getMessage());
			}
		}
	}
	
	private void registerLocationListener() {
		locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 10, networkListner);
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 10, gpsListener);
	}
	
	private void removeLocation() {
		locationManager.removeUpdates(networkListner);
		locationManager.removeUpdates(gpsListener);
	}
	
	class MyLocationListener implements LocationListener {
		
		//private String currentProvider = null;
		
        /**
         * GPS开启时触发
         */
        public void onProviderEnabled(String provider) {
        	//if (currentProvider == null) {
        	//	currentProvider = provider;
        	//}
        	//if (LocationManager.NETWORK_PROVIDER.equals(currentProvider)
        	//		&& LocationManager.GPS_PROVIDER.equals(provider)) {
        	//	currentProvider = LocationManager.GPS_PROVIDER;
        	//}
        	Log.i(TAG, "onProviderEnabled："+provider); 
        }
        
        /**
         * 位置信息变化时触发
         */
        public void onLocationChanged(Location location) {
        	//if (currentProvider != null && currentProvider.equals(location.getProvider())) {
        		currentLocation = location;
        	//}
        }
        
        /**
         * GPS状态变化时触发
         */
        public void onStatusChanged(String provider, int status, Bundle extras) {
            
        }
    
        /**
         * GPS禁用时触发
         */
        public void onProviderDisabled(String provider) {
        	//if (LocationManager.GPS_PROVIDER.equals(provider)) {
        	//	currentProvider = LocationManager.NETWORK_PROVIDER;
        	//}
        	Log.i(TAG, "onProviderDisabled："+provider); 
        }

    };
    
    private List<GpsSatellite> numSatelliteList = new ArrayList<GpsSatellite>();
    
    //状态监听
    private final GpsStatus.Listener listener = new GpsStatus.Listener() {
        public void onGpsStatusChanged(int event) {
            switch (event) {
            case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
                GpsStatus gpsStatus=locationManager.getGpsStatus(null);
                int maxSatellites = gpsStatus.getMaxSatellites();
                Iterator<GpsSatellite> iters = gpsStatus.getSatellites().iterator();
                numSatelliteList.clear();
                int count = 0;     
                while (iters.hasNext() && count <= maxSatellites) {
                	GpsSatellite s = iters.next();  
                    numSatelliteList.add(s); 
                    count++;     
                }   
                satelliteCount = numSatelliteList.size();
                break;
            }
        };
    };
}
